package com.ymttest.business.service;

import cn.ymatou.activityadmin.service.AuditApproveCall;
import cn.ymatou.activityadmin.service.CloseProductActivityCall;
import cn.ymatou.admin.service.LoginCall;
import com.ymatou.iapi.selleractivity.service.ApplyActivityCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.YmtActivity;
import com.ymttest.database.model.YmtProductsInActivity;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.YmtActivityWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminCallService {
	public static final String loginName = EnvSetup.getData("admin");
	public static final String loginPswd = EnvSetup.getData("password");

	public static List<String> getLoginCookieList(){
		return getLoginCookieList(loginName,loginPswd);
	}

	/***
	 * 获得用户cookie
	 * @param nameStr
	 * @param pswdStr
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static List<String> getLoginCookieList(String nameStr,String pswdStr){
		try{
			LoginCall loginCall = new LoginCall();
			loginCall.setData("ReturnRrl=&UserName="+nameStr+"&PassWord="+pswdStr);
			loginCall.callService();
			
			return loginCall.getCookies();
		}catch (Exception e) {
			// TODO: handle exception
			Logger.fail(e);
		}
		return null;
		
	}
	
	
/*	public static JSONObject  applyAndApproveProductInActivity(int sellerId,String productID,String activityId ) throws Exception{
		String catalogsID = new YmtCatalogsIWapper().selectCatalogsByProductId(productID).get(0).getsCatalogId();
		//1 apply 
		ActivityCallService.applyProductInActivity(sellerId, productID, catalogsID, activityId);
		//2 get productInActivityId
		int productInActivityId = new YmtProductsInActivityWapper().selectByProductId(productID).getIproductinactivityid();
		//3 approve
		return approveProductInActivity(String.valueOf(productInActivityId));
	}*/
	
	
	/***
	 * 审核通过 商品参加活动
	 * @param productInActivityId
	 * @return
	 * @throws JSONException
	 */
	
	public static JSONObject approveProductInActivity(String productInActivityId) throws JSONException{
		return approveProductInActivity(loginName,loginPswd,productInActivityId);
	}
	
	/***
	 * 审核通过 商品参加活动
	 * 根据活动开始结束时间，合理调整商品参加活动时间，保证参加活动成功  
	 * @param nameStr
	 * @param pswdStr
	 * @param productInActivityId
	 * @return
	 * @throws JSONException
	 */
	public static JSONObject approveProductInActivity(String nameStr,String pswdStr,String productInActivityId) throws JSONException{
		try {
			int activityId = new YmtProductsInActivityWapper().selectByProductInActivityId(Integer.parseInt(productInActivityId)).getIactivityid();;
			
			YmtActivity ymtActivity = new YmtActivityWapper().getRecordByActivityId(activityId);
			
			String productActivityEndTime = YMTDateUtil.getBeforeOrNextDay(YMTDateUtil.getFormatTime(ymtActivity.getDendtime(),YMTDateUtil.YMDHMS),0);
			String productActivityStartTime  = YMTDateUtil.getDateBeforeOrNextSecond(YMTDateUtil.getFormatTime(ymtActivity.getDbegintime(),YMTDateUtil.YMDHMS),1);

			Map<String, Object> mapS = MapUtil.hashMap("productBeginTime", productActivityStartTime, "productEndTime", productActivityEndTime);
			
			return approveProductInActivity(nameStr,pswdStr,productInActivityId,mapS);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/***
	 * 审核通过 商品参加活动 可传自定义时间，
	 *     比如大于活动结束时间，导致活动报名失败
	 * @param nameStr
	 * @param pswdStr
	 * @param productInActivityId
	 * @param updateMap
	 * @return
	 * @throws JSONException
	 * @throws InterruptedException 
	 */
	public static JSONObject approveProductInActivity(String nameStr,String pswdStr,String productInActivityId,Map<String, Object> updateMap) throws JSONException, InterruptedException{
		updateMap.put("productInActivityIds", new JSONArray("["+ productInActivityId +"]"));
		AuditApproveCall approveCall= new AuditApproveCall();
		Thread.sleep(500);
		List<String> cookies=getLoginCookieList(nameStr,pswdStr);
		Logger.debug("cookies:"+cookies);
		approveCall.setCookies(cookies);
		JSONObject retBean = approveCall.callAndGetReturnData(updateMap);
		
		//验证审核通过
		String msgStr = retBean.getBoolean("Success")?"活动审核通过":"活动审核不通过";
		Logger.comment("++++++"+msgStr+"++++++");
		
		return retBean;
	}
	
	
	
	
	public static int applyAndApproveInActivity(int activityId,int sellerId,String productId) throws Exception{
		ApplyActivityCall.applyProductInActivity(sellerId, productId,activityId, MapUtil.hashMap());
		YmtProductsInActivity productsInActivity = new YmtProductsInActivityWapper().selectByProductId(productId);
		int productInActivityId = productsInActivity.getIproductinactivityid();
		AdminCallService.approveProductInActivity(String.valueOf(productInActivityId));
		return productInActivityId;
	}
	
	
	
	
	public static JSONObject closeProductInActivity(String productInActivityId) throws Exception{
		return closeProductInActivity(loginName,loginPswd,productInActivityId);
	}
	
	/***
	 * 结束 商品参加活动
	 * @param nameStr
	 * @param pswdStr
	 * @param productInActivityId
	 * @return
	 * @throws JSONException
	 */
	public static JSONObject closeProductInActivity(String nameStr,String pswdStr,String productInActivityId) throws Exception{
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		Object endDate = activityProductsWapper.getByProdInActivityId(Integer.parseInt(productInActivityId)).get("end");
		
		Map<String, Object> updateMap = new HashMap<String, Object>();
		updateMap.put("search->ProductInActivityIds", new JSONArray("["+ productInActivityId +"]"));
		CloseProductActivityCall closeCall= new CloseProductActivityCall();
		closeCall.setCookies(getLoginCookieList(nameStr,pswdStr));
		
		JSONObject retBean =closeCall.callAndGetReturnData(updateMap);
		
		
		//验证结束活动
		YmtMongoDriverFactroy.isUpdate("ActivityProducts", MapUtil.hashMap("inaid",Integer.parseInt(productInActivityId))
											, MapUtil.hashMap("end",endDate));
		String msgStr = retBean.getBoolean("Success")?"结束活动成功":"结束活动失败";
		Logger.comment("++++++"+msgStr+"++++++");
		YMTDateUtil.waitTime(2);
		return retBean;
	}
	
	
	
	
	public static void main(String[] args) throws Exception {
		//applyAndApproveProductInActivity(20336378,"2309153f-4697-4543-836a-c3d3b58357dd","18946");
	}
	
}
